#!/usr/bin/env python3

import os
import re

import urllib3


def main():
    url = 'http://mirrors.ustc.edu.cn/lfs/lfs-packages/8.4/'
    cache_file = '/tmp/page-' + re.sub(r'[^a-zA-Z0-9_\-\.]+', '-', url).strip('-')
    if not os.path.exists(cache_file):
        http = urllib3.PoolManager()
        resp = http.request('GET', url)
        assert resp.status == 200
        with open(cache_file, 'wb') as _file:
            _file.write(resp.data)
    with open(cache_file, 'r') as _file:
        html = _file.read()
    packages = [i.groups()[0] for i in re.finditer(r'<a href="([a-zA-Z0-9_\-\.]+)">', html)]
    with open('/tmp/lfs-wget-list', 'w') as _file:
        for package in packages:
            _file.write(url + package + '\n')


if __name__ == '__main__':
    main()
